package com.yj.cmp.sdk.fusioncompute.network.securitygroup;

import com.yj.cmp.sdk.fusioncompute.common.utils.RequestUtils;
import com.yj.cmp.sdk.fusioncompute.network.securitygroup.dto.SecurityGroupDto;
import com.yj.cmp.sdk.fusioncompute.network.securitygroup.vo.SecurityGroupVo;
import com.yj.cmp.sdk.fusioncompute.network.securitygroup.vo.SecurityGroupsListVo;
import org.springframework.stereotype.Component;
import retrofit2.Response;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Harvey_Yang
 * @date 2021/8/28 11:16
 */
@Component
public class FcSecurityGroupApi {
    FcSecurityGroupService fcSecurityGroupService;

    public FcSecurityGroupApi(FcSecurityGroupService fcSecurityGroupService) {
        this.fcSecurityGroupService = fcSecurityGroupService;
    }

    /**
     * 安全组列表
     */
    public List<SecurityGroupsListVo.SecurityGroupsListDTO> SecurityGroupsList(Map<String, Object> headerMap, Map<String, Object> queryParam) throws Exception {
        List<SecurityGroupsListVo.SecurityGroupsListDTO> securityGroupsListDTOList = new ArrayList<>();
        Response<SecurityGroupsListVo> response = fcSecurityGroupService.securityGroupsList(headerMap, queryParam).execute();
        RequestUtils.isSuccessful(response);
        if (response.body().getTotal() <= 100) {
            if (null != response.body()) {
                return response.body().getSecurityGroups();
            }
            return new ArrayList<>(0);
        }
        if (response.body().getTotal() != null) {
            int page = (int) Math.ceil(new Double(response.body().getTotal()) / 100);
            for (int i = 2; i <= page; i++) {
                queryParam.put("offset", i);
                queryParam.put("limit", 100);
                securityGroupsListDTOList.addAll(fcSecurityGroupService.securityGroupsList(headerMap, queryParam).execute().body().getSecurityGroups());
            }
        }
        return securityGroupsListDTOList;
    }

    /**
     * 创建安全组
     */
    public SecurityGroupVo createSecurityGroup(Map<String, Object> headerMap, SecurityGroupDto securityGroupDto) throws Exception {

        return null;
    }
}
